<template>

  <div class="UserMain">
    <el-container>
    <el-aside class="document-aside" width="150px" >
      <el-scrollbar style="height: 500px" >
        <el-menu :default-active="activeIndex" class="el-menu-demo"  @select="handleSelect" router>
          <el-menu-item index="User">个人中心</el-menu-item>
          <el-menu-item index="MyQuest">我的提问</el-menu-item>
          <el-menu-item index="MyFavorite">我的收藏</el-menu-item>
        </el-menu>
      </el-scrollbar>
    </el-aside>
    <el-main><router-view></router-view></el-main>
    </el-container>
  </div>
</template>

<script>
  export default {
    name: "UserMain",
    data() {
      return {
        type:'User',
        activeIndex:'User'
      };
    },
    mounted(){
      let index = this.$store.state.activeIndex;
      if(!index){
        index = sessionStorage.getItem('activeIndex');
      }
      this.activeIndex = index;
      this.handleSelect(index,index);
      // this.activeIndex = this.$route.params.data;
      // this.type = this.$route.params.data;
      // this.$router.push(this.type)
    },
    watch :{
      '$route': function (to, from) {
        //只要上级路由传参过来就修改导航栏默认选项
        if(to.params.data){
          this.activeIndex = sessionStorage.getItem('activeIndex')
        }
        this.handleSelect(this.activeIndex,this.activeIndex);
      }
    },
    methods: {
      handleSelect(key, keyPath) {
        this.activeIndex = key;
        this.$store.state.activeIndex = key;
        sessionStorage.setItem('activeIndex',key);
        this.$router.push(key);
      }
    }
  }
</script>

<style scoped lang="scss">
  .Document{
  }
  .document-aside {
    height: 500px;
    margin-top: 10px;
  }

</style>
